package com.greenantmall.view;

import com.greenantmall.R;
import com.greenantmall.utils.UtilTools;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class CommonDialog extends Dialog implements OnClickListener,
    OnKeyListener
{
    // CheckBox mCheck;
    Button mYes, mNo;
    AutoCompleteTextView mAutoComplete;
    Context context;
    TextView mMessage;
    TextView mTitle;
    boolean isCheck = true;

    OnSureClickListener onSureClickListener;

    public interface OnSureClickListener
    {

        public void sureClicked();
    }

    public void setSureListener(OnSureClickListener sureClickListener)
    {
        this.onSureClickListener = sureClickListener;
    }


    public CommonDialog(Context context, int style, String title, String msg)
    {
        super(context, style);
        init(context,title,msg);
    }

    public void init(Context context,String title,String msg)
    {
        setDialogTitle();
        // mCheck = (CheckBox)findViewById(R.id.checkBox1);
        // mCheck.setEnabled(isCheck);
        // mCheck.setOnKeyListener(this);
        this.context = context;
        mYes = (Button) findViewById(R.id.button_sure);
        mYes.setOnClickListener(this);
        mYes.setOnKeyListener(this);
        mNo = (Button) findViewById(R.id.button_cancel);
        mNo.setOnClickListener(this);
        mNo.setOnKeyListener(this);
        this.setCanceledOnTouchOutside(false);
        mTitle = (TextView)findViewById(R.id.commondialog_title);
        mTitle.setText(title);
        mMessage=(TextView)findViewById(R.id.commondialog_message);
        mMessage.setText(msg);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }

    public void onClick(View v)
    {
        if (v.equals(mYes))
        {

            if (onSureClickListener != null)
            {
                onSureClickListener.sureClicked();
            }
            this.dismiss();
        } else if (v.equals(mNo))
        {
        	this.dismiss();
        }
    }

    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_ENTER
            || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                if (v.equals(mYes))
                {
                    if (onSureClickListener != null)
                    {
                        onSureClickListener.sureClicked();
                    }
                    this.dismiss();
                } else if (v.equals(mNo))
                {
                	this.dismiss();
                }

                return true;
            } else
            {
                return true;
            }
        }
        return false;
    }

    void setDialogTitle()
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        this.setContentView(R.layout.commondialog);
    }

    // 想要退出时聚焦到取消按钮上
    // by dong 2012-1-6
    @Override
    public void show()
    {
        super.show();
        // 默认取消按钮获得焦点
        mYes.requestFocus();
    }

}
